package com.projles.release1;

import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore.Images;
import android.provider.MediaStore.Images.Media;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.SurfaceHolder;
import android.view.SurfaceView;


public class TirarFotosActivity extends Activity implements
SurfaceHolder.Callback
{
	private Camera camera;
	private boolean isPreviewRunning = false;
	private SimpleDateFormat timeStampFormat = new SimpleDateFormat("yyyyMMddHHmmssSS");

	private SurfaceView surfaceView;
	private SurfaceHolder surfaceHolder;
	private Uri target = Media.EXTERNAL_CONTENT_URI;

	public void onCreate(Bundle icicle)
	{
	super.onCreate(icicle);
	Log.e(getClass().getSimpleName(), "onCreate");
	getWindow().setFormat(PixelFormat.TRANSLUCENT);
	setContentView(R.layout.tirar_fotos);
	surfaceView = (SurfaceView) findViewById(R.id.imagem_camera);
	surfaceHolder = surfaceView.getHolder();
	surfaceHolder.addCallback(this);
	surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
	}

	public boolean onCreateOptionsMenu(android.view.Menu menu) {
	MenuItem item = menu.add(0, 0, 0, "goto gallery");
	item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
	 public boolean onMenuItemClick(MenuItem item) {
	     Intent intent = new Intent(Intent.ACTION_VIEW, target);
	     startActivity(intent);
	     return true;
	 }
	});
	return true;
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState)
	{
	super.onRestoreInstanceState(savedInstanceState);
	}

	Camera.PictureCallback mPictureCallbackRaw = new Camera.PictureCallback() {
	public void onPictureTaken(byte[] data, Camera c) {
	 Log.e(getClass().getSimpleName(), "PICTURE CALLBACK RAW: " + data);
	 camera.startPreview();
	}
	};

	Camera.PictureCallback mPictureCallbackJpeg= new Camera.PictureCallback() {
	public void onPictureTaken(byte[] data, Camera c) {
	 Log.e(getClass().getSimpleName(), "PICTURE CALLBACK JPEG: data.length = " + data);
	 camera.startPreview();
	}
	};

	Camera.ShutterCallback mShutterCallback = new Camera.ShutterCallback() {
	public void onShutter() {
	Log.e(getClass().getSimpleName(), "SHUTTER CALLBACK");
	}
	};


	public boolean onKeyDown(int keyCode, KeyEvent event)
	{
	ImageCaptureCallback iccb = null;
	if(keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
	try {
	String filename = timeStampFormat.format(new Date());
	ContentValues values = new ContentValues();
	values.put(Images.Media.TITLE, filename);
	values.put(Images.Media.DESCRIPTION, "Imagem capturada pelo 'Time to Drink!'");
	values.put(Images.Media.MIME_TYPE, "image/jpeg");
	Uri uri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values);
	filename = timeStampFormat.format(new Date());
	iccb = new ImageCaptureCallback( getContentResolver().openOutputStream(uri));
	} catch(Exception ex ){
	ex.printStackTrace();
	Log.e(getClass().getSimpleName(), ex.getMessage(), ex);
	}
	}
	if (keyCode == KeyEvent.KEYCODE_BACK) {
	 return super.onKeyDown(keyCode, event);
	}

	if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
	 camera.takePicture(mShutterCallback, mPictureCallbackRaw, iccb, mPictureCallbackJpeg);
	 return true;
	}

	return false;
	}

	protected void onResume()
	{
	Log.e(getClass().getSimpleName(), "onResume");
	super.onResume();
	}

	protected void onSaveInstanceState(Bundle outState)
	{
	super.onSaveInstanceState(outState);
	}

	protected void onStop()
	{
	Log.e(getClass().getSimpleName(), "onStop");
	super.onStop();
	}

	public void surfaceCreated(SurfaceHolder holder)
	{
	Log.e(getClass().getSimpleName(), "surfaceCreated");
	camera = Camera.open();
	}

	public void surfaceChanged(SurfaceHolder holder, int format, int w, int h)
	{
	Log.e(getClass().getSimpleName(), "surfaceChanged");
	if (isPreviewRunning) {
	 camera.stopPreview();
	}
	Camera.Parameters p = camera.getParameters();
	p.setPreviewSize(w, h);
	camera.setParameters(p);
	try {
		camera.setPreviewDisplay(holder);
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	camera.startPreview();
	isPreviewRunning = true;
	}

	public void surfaceDestroyed(SurfaceHolder holder)
	{
	Log.e(getClass().getSimpleName(), "surfaceDestroyed");
	camera.stopPreview();
	isPreviewRunning = false;
	camera.release();
	}
	
	public class ImageCaptureCallback implements PictureCallback  {

		private OutputStream filoutputStream;
		public ImageCaptureCallback(OutputStream filoutputStream) {
		this.filoutputStream = filoutputStream;
		}
		@Override
		public void onPictureTaken(byte[] data, Camera camera) {
		try {
		Log.v(getClass().getSimpleName(), "onPictureTaken=" + data + " length = " + data.length);
		filoutputStream.write(data);
		filoutputStream.flush();
		filoutputStream.close();
		} catch(Exception ex) {
		ex.printStackTrace();
		}
		}
		}
	
}